﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using mqtt;
using OfficeOpenXml;
using Simple.Common.EventBus;
using Simple.Common.Filters;

namespace Simple.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [DisabledRequestRecord]
    public class TestController : ControllerBase
    {
        private readonly TestService _testService;
        private readonly IMapper _mapper;
        private readonly IEventPublisher _eventPublisher;

        public TestController(TestService testService, IMapper mapper, IEnumerable<ITestService> testServices, ITestService test, IEventPublisher eventPublisher)
        {
            _testService = testService;
            _mapper = mapper;
            _eventPublisher = eventPublisher;
        }

        /// <summary>
        /// 测试获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get()
        {
            var @event = new DeviceEventModel();
            _eventPublisher.PublishAsync(@event).GetAwaiter().GetResult();

            return _testService.Get();
        }

        [HttpGet]
        public string GetToken()
            => _testService.GetToken();

        [Authorize]
        [HttpGet]
        public string Auth()
            => _testService.Get();

        [HttpGet]
        public string Throw()
        {
            throw new AppResultException(new AppResult(401, "测试401返回"));
            throw new Exception("测试异常");
        }

        [HttpGet]
        public string Throw2(string s = "12334")
        {
            throw new Exception("测试异常");
        }

        [HttpPost]
        public IActionResult MqttTest()
        {
            // 创建一个新的Excel包
            using (var package = new ExcelPackage())
            {
                // 添加一个工作表
                var worksheet = package.Workbook.Worksheets.Add("Sheet1");

                // 创建数据
                var data = new List<List<object>>
            {
               new List<object> { "序号", "姓名", "实际出勤天数" },
            };

                var date = DateTime.Now;
                var year = date.Year;
                var month = date.Month;
                var days = DateTime.DaysInMonth(year, month);



                for (var i = 1; i <= days; i++)
                {
                    DateTime currtime = DateTime.Parse($"{year}-{(month).ToString().PadLeft(2, '0')}-{i.ToString().PadLeft(2, '0')}");
                    string dayOfWeek = currtime.DayOfWeek.ToString();
                    
                }





                // 将数据写入工作表
                int row = 1;
                foreach (var rowData in data)
                {

                    int col = 1;
                    foreach (var cellData in rowData)
                    {
                        worksheet.Cells[row, col].Value = cellData;
                        col++;
                    }
                    row++;
                }

                // 将Excel包保存为字节数组
                var excelBytes = package.GetAsByteArray();

                // 设置响应的Content-Disposition标头，指示文件名和下载
                var fileName = "output.xlsx";
                return File(excelBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
            }
        }
    }
}
